<template>
  <div class="app-container">
    <!-- 表格-条件筛区域  -->
    <div class="filter-container">
      <el-input placeholder="请输入证书名称" v-model="searchParam.SEARCH_LIKE_cert_name" clearable style="width: 200px;"
                class="filter-item" @keyup.enter.native="handleFilter"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter"> 搜索</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus"
                 @click="handleCreate"> 新增
      </el-button>
    </div>

    <!-- 表格-内容区域 -->
    <el-table v-loading="listLoading" :key="tableKey" :data="tableList" border fit highlight-current-row
              style="width: 100%;">
      <el-table-column v-for="column in columns" :key="column.prop"
                       :prop="column.prop"
                       :label="column.label"
                       :width="column.width"
                       :align="column.align?column.align:'center'"
                       :min-width="column.minWidth"
                       :sortable="column.sortable"
                       :fixed="column.fixed"
                       :type="column.type" :formatter="column.formatter">
        <template slot-scope="scope">
          <!-- 行内显示图片 -->
          <img v-if="column.imgFlag==true"
               :src=" scope.row[column.prop].indexOf('http') >-1 ?  scope.row[column.prop] : ( CMBASE.baseFileContext + scope.row[column.prop] ) "
               width="60" height="60" class="img"/>
          <!-- 行内显示开关按钮 -->
          <el-switch v-else-if="column.switchFlag==true" v-model="scope.row[column.prop]" :active-value="1"
                     :inactive-value="0" disabled/>
          <!-- 行内显示tag标签 -->
          <span v-else-if="column.tagFlag==true">
                        <span v-for="item in column.tagFormatter" :key="item.value">
                            <el-tag v-if="item.value==scope.row[column.prop]" :type="item.type">{{ item.text }}</el-tag>
                        </span>
                    </span>
          <!-- 行内格式自定义 -->
          <span v-else-if="column.formatter"> {{ column.formatter(scope.row) }}</span>
          <!--默认显示文本-->
          <span v-else>{{ scope.row[column.prop] }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="240" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" class="el-button--primary" @click="handleUpdate(scope.row)"> 编辑
          </el-button>
          <el-button type="text" icon="el-icon-delete" class="el-button--danger" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格-页脚区域 -->
    <pagination v-show="tableTotal>0" :total="tableTotal" :page.sync="pageRequest.pageNum"
                :limit.sync="pageRequest.pageSize" @pagination="getList"/>

    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'" width="75%" top="5vh" :visible.sync="dialogFormVisible"
               :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="140px" :rules="dataFormRules" ref="dataForm" style="width: 80%">

        <el-form-item label="证书来源" prop="certSource">
          <el-radio-group  v-model="dataForm.certSource">
            <el-radio :label="1">考试</el-radio>
            <el-radio :label="2">课程</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="证书类型" prop="certType" >
          <el-radio-group v-model="dataForm.certType">
            <el-radio :label="1">Vastbase</el-radio>
            <el-radio :label="2">openGauss</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="orderNo">
          <el-input-number v-model="dataForm.orderNo" :min="1"  label="排序"></el-input-number>
        </el-form-item>
        <el-form-item label="证书级别" prop="certLevel" v-if="dataForm.certSource == 1" >
          <el-switch v-model="level" :active-value="false" active-text="允许跨级" :inactive-value="true" inactive-text="不允许跨级" ></el-switch>
          <p v-if="level" > 证书级别
            <el-select v-model="dataForm.certLevel" >
              <el-option :value="1" label="初级"></el-option>
              <el-option :value="2" label="中级"></el-option>
              <el-option :value="3" label="高级"></el-option>
            </el-select>
          </p>
        </el-form-item>
        <el-form-item label="证书宽度" prop="certWidth">
          <el-input v-model="dataForm.certWidth"  auto-complete="off" placeholder="请输入证书宽度"> <template slot="append">px</template></el-input>
        </el-form-item>
        <el-form-item label="编号规则" prop="noRule">
          <el-input v-model="dataForm.noRule"  auto-complete="off" placeholder="请输入证书编号规则 - 例如：CA/CP/CE" />
        </el-form-item>
        <el-form-item label="证书名称" prop="certName">
          <el-input v-model="dataForm.certName"  type="text" auto-complete="off" maxlength="32" show-word-limit placeholder="请输入证书名称"/>
        </el-form-item>
        <el-form-item label="证书介绍" prop="certDesc">
          <el-input v-model="dataForm.certDesc" type="textarea" auto-complete="off" placeholder="请输入证书简介"/>
        </el-form-item>
        <el-form-item label="证书详情" prop="certDetail">
          <tinymce :height="300" v-model="dataForm.certDetail" :id="'certDetail'" :value="dataForm.certDetail"
                   v-if="dialogFormVisible"></tinymce>
        </el-form-item>
        <el-form-item label="证书申请条件" prop="certDetail">
          <tinymce :height="300" v-model="dataForm.applyCondition" :id="'applyCondition'" :value="dataForm.applyCondition"
                   v-if="dialogFormVisible"></tinymce>
        </el-form-item>
        <el-form-item label="证书标志" prop="certIcon">
          <cm-upload v-model="dataForm.certIcon"></cm-upload>
        </el-form-item>
        <el-form-item label="证书图片(带章)" prop="certImg">
          <cm-upload v-model="dataForm.certImg"></cm-upload>
        </el-form-item>
          <el-form-item label="证书图片(不带章)" prop="certImg">
              <cm-upload v-model="dataForm.certImgNoSignet"></cm-upload>
          </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="closeDialog()">取消</el-button>
        <el-button size="mini" @click.native="saveFormData()" :loading="editLoading" type="primary">提交</el-button>
      </div>
    </el-dialog>


  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 分页栏信息
import CmUpload from '@/components/CmUpload' // 上传图片组件引入
import Tinymce from '@/components/CmUEditor' // 富文本编辑器引入
export default {
  name: 'certification',
  components: {
    Pagination,
    CmUpload,
    Tinymce,
  },
  directives: {},
  filters: {},
  data() {
    return {
      tableKey: 0,
      tableList: [],//表格数据源
      tableTotal: 0,//表格总条数数据
      tinymceKey1: 1,//表格总条数数据
      tinymceKey2: 1,//表格总条数数据
      listLoading: true,
      pageRequest: {pageNum: 1, pageSize: 10,},
      orderParam :{order_no: true},
      searchParam: {},
      dialogFormVisible: false,//默认不显示弹出框
      operation: true, // true:新增, false:编辑
      editLoading: false,//保存表单时loading框
      columns: [
        {prop: 'certName', label: '证书名称', sortable: false},
        {
          prop: 'certType', label: '证书类型', sortable: false, tagFlag: true,
          tagFormatter: [
            {text: 'VastBase', value: 1, type: 'success'},
            {text: 'OpenGauss', value: 2, type: 'info'}
          ]
        },
        {prop: 'certIcon', label: '证书标志', sortable: false, imgFlag: true},
        {prop: 'orderNo', label: '排序', sortable: false},
        {prop: 'createTime', label: '创建时间', sortable: false},
      ],
      fileList: [],
      // 新增编辑界面数据
      dataForm: {},
      //表单检验规则
      dataFormRules: {
        // certType: [{required: true, message: '请输入证书类型 1 vastbase 2 opengause', trigger: 'blur'}],
        certSource: [{required: true, message: '请选择证书来源 课程 或者 考试', trigger: 'blur'}],
        certName: [{required: true, message: '请输入证书名称', trigger: 'blur'}],
        certDesc: [{required: true, message: '请输入证书介绍', trigger: 'blur'}],
        // certIcon: [{required: true, message: '请输入证书标志', trigger: 'blur'}],
        certImg: [{required: true, message: '请输入证书图片', trigger: 'blur'}],
        certWidth: [{required: true, message: '请输入宽度', trigger: 'blur'}],
        noRule: [{required: true, message: '请输入证书编号规则', trigger: 'blur'}],
      },
      level:null,
    }
  },
  created() {
    this.pageRequest.orderParam = this.orderParam
    this.getList();
  },
  activated() {
    this.tinymceKey1++
    this.tinymceKey2++
  },
  methods: {
    //获取表格数据
    getList() {
      this.listLoading = true
      //【API请求】----获取表格列表数据
      this.$api.getZCertificateList(this.pageRequest).then(response => {
        this.tableList = response.data.content
        this.tableTotal = response.data.total
        this.listLoading = false;
      }).catch((res) => {
        this.$message({message: '列表获取失败', type: 'error'})
      })
    },
    //表格数据筛选
    handleFilter() {
        this.pageRequest = {
            pageNum: 1,
            pageSize: 10,
        }
      this.pageRequest.searchParam = this.searchParam
      this.pageRequest.orderParam = this.orderParam
      this.getList()
    },
    //【按钮点击】--- 弹出新增表单
    handleCreate() {
      this.operation = true;
      this.initDataForm();
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
        this.editLoading = false;
      })
    },
    //【按钮点击】--- 弹出编辑表单
    handleUpdate(row) {
      this.operation = false;
      this.dataForm = Object.assign({}, row)
      if (this.dataForm.certLevel===0){
        this.level = false
      }else{
        this.level = true
      }
      //显示编辑菜单
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
        this.editLoading = false;
      })
    },
    //保存表单数据
    saveFormData() {
      console.log(this.level ,"level")
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false;
        }
        if (this.level === false){
          this.dataForm.certLevel=0
        }else if(this.level === true)  {
          this.dataForm.certLevel=0?this.dataForm.certLevel=1:this.dataForm.certLevel
        }
        this.editLoading = true;
        //【API请求】----保存数据
        this.$api.saveZCertificate(this.dataForm).then(() => {
          this.editLoading = false;//关闭loading框
          this.dialogFormVisible = false;//关闭表单页面
          this.initDataForm();//重置表单数据
          this.getList();//重新获取表格数据
          this.$notify({title: '成功', message: '保存成功', type: 'success', duration: 2000})
        })
      })
    },
    //【按钮点击】---删除行
    handleDelete(row) {
      this.$confirm('确认删除该记录吗？', '提示', {}).then(() => {
        //【API请求】---- 删除数据
        this.$api.deleteZCertificate({ids: row.id}).then(() => {
          this.$message({message: '操作成功', type: 'success'})
            this.pageRequest.pageNum = 1
          this.getList();
        })
      }).catch(() => {
        // this.$message({message: '操作失败', type: 'error'})
      });
    },
    //初始化表单数据，重新渲染
    initDataForm() {
      this.dataForm = {
        certType: 1,
        orderNo: '',
        certName: '',
        certDesc: '',
        certLevel:'',
        certDetail: '',
        applyCondition: '',
        certImgNoSignet:'',
        id: '',
        noRule:'',
        certSource:'',
        certIcon: '',
        certImg: '',
        certWidth:'',
      };
    },
    //关闭表单弹出框
    closeDialog() {
      this.$refs['dataForm'].resetFields();
      this.dialogFormVisible = false
    },

  }


}
</script>
